package com.cheney.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheney.demo.mapper.TestUserMapper;
import com.cheney.demo.model.TestUser;
import com.cheney.demo.service.TestNameService;
import com.cheney.demo.service.TestUserService;
import com.cheney.log.annotation.AuditLog;
import com.cheney.redisson.annotation.Lock;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author si.chen
 * @date 2021/1/1 14:30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TestUserServiceImpl extends ServiceImpl<TestUserMapper, TestUser> implements TestUserService {

    @Autowired
    private TestNameService testNameService;

    @SneakyThrows
    @Override
    @Lock(keys = "#testUser.name")
    @AuditLog(operation = "分布式锁测试doSomething")
    public void doSomething(TestUser testUser) {
        System.out.println(Thread.currentThread().getName() + "进来了");
        //执行时间<attemptTimeout：正常
        //执行时间>=attemptTimeout && 执行时间<lockWatchdogTimeout：锁被其他线程占用
        //执行时间>=lockWatchdogTimeout：锁自动释放，重新争取锁
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getName() + "离开了");
    }

    @Override
    public List<TestUser> list() {
        return super.list();
    }
}
